/**
 * 
 */
package controller.xml;

import java.util.HashSet;
import java.util.Set;

import model.solver.ImportESolver;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Use to parse a set of {@link ImportESolver} exported in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class ExportedESolverHandler extends DefaultHandler {

	private ExportedESolverXMLStatus	status		= ExportedESolverXMLStatus.NONE;
	protected ImportESolverXMLHandler	delegate	= null;
	private Set<ImportESolver>			solvers		= new HashSet<ImportESolver>();


	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		System.out.println("Characters: " + new String(ch, start, length));
		if (delegate != null)
			if (delegate.characters(ch, start, length))
				return;
		throw new SAXException("unnexpecte behavior");
	}

	@Override
	public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		System.out.println("Start element: " + arg2);

		if (delegate != null)
			if (delegate.startElement(arg0, arg1, arg2, arg3))
				return;

		startElement2(arg0, arg1, arg2, arg3);
	}


	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		System.out.println("End element: " + qName);

		if (delegate != null)
			if (delegate.endElement(uri, qName, qName))
				return;
		endElement2(uri, qName, qName);
	}

	public void endElement2(String uri, String localName, String qName) throws SAXException {
		System.out.println("session end element " + qName);

		switch (status) {
		case NONE:
			// tolere la balise set solver mais l'ignore
			if (!qName.equals(XMLBaliseFormat.SET_IMPORTESOLVER))
				throw new SAXException("no element to end");
			else
				return;

		case SOLVER:
			if (qName.equals(XMLBaliseFormat.IMPORTESOLVER))
			{// ferme solver
				status = ExportedESolverXMLStatus.NONE;
				ImportESolver use = delegate.getSolver();
				if (use == null)
					throw new SAXException("problem SOLVER");

				solvers.add(use);
				delegate = null;
				return;
			}
			throw new SAXException("end of SOLVER was expected");
		}

		throw new SAXException("an other end of element was expected");
	}


	public void startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		System.out.println("start element session" + arg2);

		if (status != ExportedESolverXMLStatus.NONE)
			throw new SAXException("already have a started element");

		if (arg2.equals(XMLBaliseFormat.IMPORTESOLVER))
		{// balise solver
			// set les donnees pour recupere le solver
			status = ExportedESolverXMLStatus.SOLVER;
			delegate = new ImportESolverXMLHandler();
			return;
		}

		if (arg2.equals(XMLBaliseFormat.SET_IMPORTESOLVER))
			// sert juste a eviter le fichier xml vide -> ignorer
			return;

		// peut pas commencer balise qui existe pas
		throw new SAXException("unexpected balise: " + arg2);
	}

	/**
	 * Return the solver parsed.
	 * 
	 * @return the solver parsed.
	 */
	public Set<ImportESolver> getSolvers() {
		return solvers;
	}


	@Override
	public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
		System.out.println("White space: " + new String(ch, start, length));
	}


}
